package com.guochu.utils;

import java.security.SecureRandom;
import java.util.Random;

public class RandomUtils {
    /**
     * 创建随机验证码
     *
     * @param type   1.纯数字，2.数字字母混合，3，纯字母
     * @param length 验证码长度
     * @return 加密后的字符串
     */
    public static String getRandomCode(int type, int length) {
        Random RANDOM = new SecureRandom();
        String Symbols = "";
        if (type == 1) {
            Symbols = "0123456789";
        } else if (type == 2) {
            Symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        } else {
            Symbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        char[] nonceChars = new char[length];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = Symbols.charAt(RANDOM.nextInt(Symbols.length()));
        }
        return new String(nonceChars);
    }
}
